﻿/*
 * @(#)CodeOptionTag.java 1.0 2005-3-2
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vandagroup.twpos.taglib;


import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.TextTag;
import org.apache.struts.taglib.nested.NestedNameSupport;
import org.apache.struts.taglib.nested.NestedPropertyHelper;
import org.apache.struts.taglib.nested.NestedPropertySupport;
import org.apache.struts.util.RequestUtils;

import com.vanda.db.cache.CodeHelper;
import com.vanda.db.cache.CodeInfo;
/**
 * 类说明:
 * 代码表标签
 * @author 蒋勇
 * <P>@version 1.0 2005-3-14
 */
public class CodeOptionTag extends TextTag implements NestedNameSupport {
	private String codeName; //代码表名字
	private String codeValue; //代码表的值
	private String partition = ""; //分隔符
	private String filter; //过滤器
	private String filterColName; //过滤器字段
	/**
	 * 方法说明:
	 * 设置tag
	 * @param request
	 * @param tag
	 * @author 蒋勇 2005-4-11
	 */
	public static void setNestedProperties(
		HttpServletRequest request,
		NestedPropertySupport tag,
		String tagName) {
		boolean adjustProperty = true;
		/* if the tag implements NestedNameSupport, set the name for the tag also */
		if (tag instanceof NestedNameSupport) {
			NestedNameSupport nameTag = (NestedNameSupport) tag;
			if (nameTag.getName() == null
				|| Constants.BEAN_KEY.equals(nameTag.getName())) {
				nameTag.setName(
					NestedPropertyHelper.getCurrentName(
						request,
						(NestedNameSupport) tag));
			} else {
				adjustProperty = false;
			}
		}
		/* get and set the relative property, adjust if required */
		String property = tag.getProperty();
		if (adjustProperty) {
			tagName =
				NestedPropertyHelper.getAdjustedProperty(request, tagName);
		}
		tag.setProperty(property);
	}
	public static String getNestedName(
		PageContext pageContext,
		String formName,
		String property,
		HttpServletRequest request,
		String propertyName)
		throws JspException {
		boolean bNested = false;
		//取得propertyName的值，如果没有，则转为嵌套name
		try {
			Object obj =
				(String) RequestUtils.lookup(
					pageContext,
					formName,
					propertyName,
					null);
		} catch (Exception e) {
			bNested = true;
		}
		if (bNested == false) {
			return propertyName;
		}
		int index = property.lastIndexOf(".");
		if (index > 0) {
			return property.substring(0, index + 1) + propertyName;
		}
		return propertyName;
	}
	
	public int doStartTag() throws JspException{
		// request
		HttpServletRequest request =
			(HttpServletRequest) pageContext.getRequest();
		// set the properties
		NestedPropertyHelper.setNestedProperties(request, this);
		Object _defaultValue = null;
		if (property != null) {
			_defaultValue =
				RequestUtils.lookup(pageContext, name, property, null);
		}
		StringBuffer html = new StringBuffer();
		CodeInfo _codeInfo = CodeHelper.getCode(codeName);
		String _filterValue = "";
		int filterIndex = -1;
		if (filter != null) {
			filter =
				getNestedName(pageContext, name, property, request, filter);
			_filterValue =
				(String) RequestUtils.lookup(pageContext, name, filter, null);
			if (_filterValue != null && _filterValue.length() > 0)
				filterIndex =
					((Integer) _codeInfo.getCodeIndex().get(filterColName.toLowerCase()))
						.intValue();
		}
		
		int _keyIndex =
			((Integer) _codeInfo.getCodeIndex().get(_codeInfo.getKey().toLowerCase()))
				.intValue();
		int[] _valueIndex = null;
		if (codeValue.indexOf(",") >= 0) {
			StringTokenizer _st = new StringTokenizer(codeValue, ",");
			_valueIndex = new int[_st.countTokens()];
			for (int i = 0; _st.hasMoreElements(); i++) {
				String _codeValue = (String) _st.nextElement();
				Integer _index =
					(Integer) _codeInfo.getCodeIndex().get(_codeValue);
				if (_index == null)
					return 0;
				_valueIndex[i] = _index.intValue();
			}
		} else {
			_valueIndex = new int[1];
			_valueIndex[0] =
				((Integer) _codeInfo.getCodeIndex().get(codeValue)).intValue();
		}
		
		ArrayList _list = getCodeList(_codeInfo);
		
		StringBuffer _values = new StringBuffer();
		for (int i = 0; i < _list.size(); i++) {
			ArrayList _item = (ArrayList) _list.get(i);
			for (int j = 0; j < _valueIndex.length; j++) {
				_values.append(_item.get(_valueIndex[j]) + partition);
			}
			_values.setLength(_values.length() - partition.length());
			Object _key = _item.get(_keyIndex);
			if (_filterValue != null && filterIndex >= 0) {
				String _ttt = (String) _item.get(filterIndex);
				if (!_filterValue.equals(_ttt)) {
					_values.setLength(0);
					continue;
				}
			}
			if (_defaultValue != null && _defaultValue.equals(_key)) {
				html.append(
					"<option selected value=\""
						+ _key
						+ "\">"
						+ _values
						+ "</option>\n");
			} else {
				html.append(
					"<option value=\""
						+ _key
						+ "\">"
						+ _values
						+ "</option>\n");
			}
			_values.setLength(0);
		}
		try {
			pageContext.getOut().write(html.toString());
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException("write error!");
		}
		return EVAL_BODY_INCLUDE;
	}
	
	protected ArrayList getCodeList(CodeInfo _codeInfo){
		return _codeInfo.getCodeList();
	}
	/**
	 * @return
	 */
	public String getCodeName() {
		return codeName;
	}
	/**
	 * @return
	 */
	public String getCodeValue() {
		return codeValue;
	}
	/**
	 * @param string
	 */
	public void setCodeName(String string) {
		codeName = string;
	}
	/**
	 * @param string
	 */
	public void setCodeValue(String string) {
		codeValue = string;
		if(codeValue != null){
			codeValue = codeValue.toLowerCase();
		}
	}
	/**
	 * @return
	 */
	public String getPartition() {
		return partition;
	}
	/**
	 * @param string
	 */
	public void setPartition(String string) {
		partition = string;
	}
	/**
	 * Release any acquired resources.
	 */
	public void release() {
		super.release();
		partition = "";
	}
	/**
	 * @return
	 */
	public String getFilter() {
		return filter;
	}
	/**
	 * @param string
	 */
	public void setFilter(String string) {
		filter = string;
	}
	/**
	 * @return
	 */
	public String getFilterColName() {
		return filterColName;
	}
	/**
	 * @param string
	 */
	public void setFilterColName(String string) {
		filterColName = string;
	}
}
